//显示树状图
define(function(require, exports, module) {
	/*这个数跟我们传统的树有点不一样，就是剖通的树创建一个节点默认就是子节点，
	这个里可以强制创建一个空的父节点*/
	var TREE = {
		jsonObj: '',
		c_root: 'tree', //树的根目录
		c_item: {
			c_: 'js_c_item', //节点
			c_parent: 'js_c_parent', //标记节点为父节点
			c_open: 'js_open',
			c_close: 'js_close',
			//节点里面的工具条
			tool: {
				c_: 'js_c_item_tool',
				//添加操作按钮
				add: {
					c_: 'js_c_item_tool_add', //按钮id
					//添加按钮响应显示的添加操作选择
					c_choose: {
						c_: 'js_c_item_add_choose', //选择模块
						item: 'js_c_item_tool_add_item', //添加普通节点的操作
						root: 'js_c_item_tool_add_root' //添加跟节点操作
					}
				},
				//删除
				del: {
					c_: 'js_c_item_tool_del', //按钮id
					c_confirm: {
						c_: 'js_c_item_c_confirm', //选择模块
						item: 'js_c_item_tool_del_item' //添加普通节点的操作
					}
				},
				//修改
				edit: {
					c_: 'js_c_item_tool_edit' //按钮id
				},
				//查询
				search: {
					c_: 'js_c_item_tool_search' //按钮id
				}
			}
		},
		//所有的父类的标签的权限名字都要用这个
		parent_name: '0-0-0',
		/*初始化函数*/
		init: function(jsonObj) {
			TREE.jsonObj = jsonObj;
			var $root = $('.' + TREE.c_root); //获取到最上层的根节点
			var $root_ul = $('<ul></ul>').appendTo($root); //创建一个跟ul标签
			$.each(TREE.jsonObj, function(ids, item) {
				if (item.pid == 0) {
					var $item = TREE.create_li.create_item(item).root_item();
					var $ul = TREE.create_li.create_item(item).li_ul();
					$ul.appendTo($item);
					$item.appendTo($root_ul);
					if (TREE.is_have_child(item.id)) {
						TREE.careateDom(item, $ul);
					}
				}
			});
		},
		
		choose_listener:function(){
			$(document).on('click', '.' + TREE.c_root + ' .' + TREE.c_item.c_, function(e) {
				var $this = $(this);
				if($this.hasClass('active')){
					$this.removeClass('active');
					$this.closest('li').find('.' + TREE.c_item.c_).removeClass('active');
				}else{
					$this.addClass('active');
					$this.closest('li').find('.' + TREE.c_item.c_).addClass('active');
				}
			});
		},
		close_listener:function(){
			$(document).off('click', '.' + TREE.c_root + ' .' + TREE.c_item.c_ + '.' + TREE.c_item.c_parent);
			$(document).off('mousemove', '.' + TREE.c_root + " li>span." + TREE.c_item.c_);
		},
		/*给这颗树添加监听*/
		listener: function() {
			/*工具条的鼠标滑过监听*/
			var $before = {
				tool: null,
				add: null,
				del: null
			};

			/*给父标签添加点击监听*/
			$(document).on('click', '.' + TREE.c_root + ' .' + TREE.c_item.c_ + '.' + TREE.c_item.c_parent, function(e) {
				var children = $(this).parent('li').find(
					'> ul > li');
				if (children.is(":visible")) {
					children.hide('fast');
					$(this).find('>i').addClass(
						'icon-plus-sign ' + TREE.c_item.c_open).removeClass(
						'icon-minus-sign ' + TREE.c_item.c_close);
				} else {
					children.show('fast');
					$(this).find('>i').addClass(
						'icon-minus-sign ' + TREE.c_item.c_close).removeClass(
						'icon-plus-sign ' + TREE.c_item.c_open);
				}
				e.stopPropagation();
			});

			/*给li标签添加鼠标进入事件的监听*/
			$(document).on('mousemove', '.' + TREE.c_root + " li>span." + TREE.c_item.c_, function(e) {
				if ($before.tool != null) {
					$before.tool.css('visibility', 'hidden');
				}
				if ($before.add != null) {
					$before.add.css('display', 'none');
				}
				if ($before.del != null) {
					$before.del.css('display', 'none');
				}
				$before.tool = $(this).closest('li').find(">span." + TREE.c_item.tool.c_);
				$before.tool.css('visibility', 'visible');
			});
			

			/*添加按钮的点击监听*/
			$(document).on('click', '.' + TREE.c_item.tool.add.c_, function(e) {
				//添加选择界面
				$before.add = $(this).closest('li').find(">span." + TREE.c_item.tool.add.c_choose.c_);
				$before.add.css('display', 'inline-block');
			});
			/*添加权限监听*/
			$(document).on('click', 'span .' + TREE.c_item.tool.add.c_choose.item, function(e) {
				TREE.add_item($(this).attr('data-id'), $(this), 1);
			});
			/*添加分组监听*/
			$(document).on('click', 'span .' + TREE.c_item.tool.add.c_choose.root, function(e) {
				TREE.add_item($(this).attr('data-id'), $(this), 2);
			});
			/*修改按钮的监听*/
			$(document).on('click', '.' + TREE.c_item.tool.edit.c_, function(e) {
				TREE.edit_item($(this).attr('data-id'), $(this));
			});
			/*删除监听*/
			$(document).on('click', '.' + TREE.c_item.tool.del.c_, function(e) {
				//删除选择界面
				$before.del = $(this).closest('li').find(">span." + TREE.c_item.tool.del.c_confirm.c_);
				$before.del.css('display', 'inline-block');
			});

			/*确定删除监听*/
			$(document).on('click', '.' + TREE.c_item.tool.del.c_confirm.item, function(e) {
				TREE.del_item($(this).attr('data-id'), $(this));
			});

			/*查看监听*/
			$(document).on('click', '.' + TREE.c_item.tool.search.c_, function(e) {
				TREE.search_item($(this).attr('data-id'), $(this));
			});
		},
		/*添加一个权限
		 * $obj:当前点击的对象(li标签)
		 * $id:当前权限的id
		 * $tag:需要创建添加页面的类型 1（权限类型）2（分组类型）
		 */
		add_item: function($id, $obj, tag) {
			TREE.disable_tool.off($obj.closest('li').find('>span.' + TREE.c_item.tool.c_));
			//当前点击的li标签
			var $this_li = $obj.closest('li');
			//当前显示的图标
			var $this_span_i = $this_li.find(">span." + TREE.c_item.c_ + ">i");
			//判断当前的li是打开的还是关闭的，如果是关闭的话就打开它
			if ($this_span_i.hasClass(TREE.c_item.c_open)) {
				$this_li.find(">ul>li").show('fast');
				$this_span_i.addClass("icon-minus-sign " + TREE.c_item.c_close).removeClass(
					"icon-plus-sign " + TREE.c_item.c_open);
			}
			var $now_li = TREE.create_li.create_item().null_item();
			/*表单处理
			 *$fObj:当前表单对象
			 */
			var form = TREE.create_form(function($fObj) {
				var title = $fObj.find('input[name="title"]').val();
				var name = $fObj.find('input[name="name"]').val();
				$$.ajax({
					url: config.APP+'/AuthRule/insert',
					data: {
						pid: $id,
						title: title,
						name: name
					},
					success: function(item_string) {
						if (item_string != 0) {
							try {
								var jsonObj = eval("(" + item_string + ")");
								$now_li.remove();
								if (tag == 1) {
									var chlid = TREE.create_li.create_item(jsonObj).chlid_item();
									$this_li.find(">ul").prepend(chlid);
								} else {
									var chlid = TREE.create_li.create_item(jsonObj).parent_item();
									var $ul = TREE.create_li.create_item(jsonObj).li_ul();
									$ul.appendTo(chlid);
									$this_li.find(">ul").prepend(chlid);
								}
							} catch (e) {
								system_error_hint("系统错误");
							}
						} else {
							system_error_hint("添加失败");
						}
					}
				});
			}, function($cObj) {
				$now_li.remove();
			});
			
			if (tag == 1) {
				$now_li.find('>span.' + TREE.c_item.c_).prepend(form.add_form());
			} else {
				$now_li.find('>span.' + TREE.c_item.c_).prepend(form.add_gourp_form());
			}
			$this_li.find('>ul').prepend($now_li);
		},
		
		/*删除一个对象*/
		edit_item: function($id, $obj) {
			$$
				.ajax({
					url: config.APP+"/AuthRule/bean",
					data: {
						id: $id
					},
					dataType: 'json',
					success: function(obj) {
						//当前操作的li标签
						var $this_li = $obj.closest('li');
						var $this_item = $this_li.find('>span.' + TREE.c_item.c_);
						var before_content = $this_item.html(); //编辑之前，保存前面的内容
						//编辑框的文本
						var form = TREE.create_form(function($cObj) {
							$$
								.ajax({
									url: config.APP+"/AuthRule/update",
									data: {
										id: $id,
										title: $cObj.find('input[name="title"]').val(),
										name: $cObj.find('input[name="name"]').val()
									},
									success: function(tag) {
										if (tag == 1) {
											var $now_before = $(before_content).text($cObj.find('input[name="title"]').val());
											$this_item.html("");
											$this_item.append($now_before);
											system_success_hint('修改成功');
										} else {
											system_error_hint('修改失败');
										}
									}
								});
						}, function($fObj) {
							$this_item.html(before_content);
						}).edit_form(obj);
						$this_item.html("");
						$this_item.append(form);
						/*监听取消按钮*/
						$this_li.find('.small_btn.cancel').click(function(e) {
							e.stopPropagation();
						});
						/*监听编辑框*/
						$this_li.find('.small_edit').click(function(e) {
							e.stopPropagation();
						});
					}
				});
		},
		/*修改一个对象*/
		del_item: function($id, $obj) {
			$$.ajax({
				url:  config.APP+'/AuthRule/del',
				data: {
					id: $id
				},
				success: function(result) {
					if (result == 1) {
						$obj.closest('li').remove();
						system_success_hint("删除成功");
					} else {
						system_error_hint("删除失败");
					}
				}
			});
		},
		/*查看一个对象*/
		search_item: function($id, $obj) {
			$$
				.ajax({
					url:  config.APP+"/AuthRule/bean",
					data: {
						id: $id
					},
					dataType: 'json',
					success: function(obj) {
						//当前操作的li标签
						var $this_li = $obj.closest('li');
						var $this_item = $this_li.find('>span.' + TREE.c_item.c_);
						var before_content = $this_item.html(); //编辑之前，保存前面的内容
						//编辑框的文本
						var form = TREE.create_form(function($fObj) {
							$this_item.html(before_content);
						}).search_form(obj);

						$this_item.html("");
						$this_item.append(form);
					}
				});
		},
		/*创建form表单*/
		create_form: function(con_fun, can_fun) {
			return {
				add_form: function() {
					var form = $('<form></form>');
					var name = $("<input type='text' name='name' placeholder='权限' class='small_edit'/>");
					var title = $("<input type='text' name='title' placeholder='名字' class='small_edit'/>");
					var confirm = $("<a class='small_btn '>添加</a>");
					var cancel = $("<a class='small_btn cancel'>取消</a>");
					form.append(title);
					form.append(name);
					form.append(confirm);
					form.append(cancel);
					confirm.click(function() {
						var title = $(this).closest('form').find('input[name="title"]').val();
						var name = $(this).closest('form').find('input[name="name"]').val();
						if (title == '') {
							system_error_hint("名称不能为空");
							return;
						}
						if (name == '') {
							system_error_hint("权限不能为空");
							return;
						}
						con_fun(form);
					});
					cancel.click(function() {
						can_fun(form);
					});
					return form;
				},
				add_gourp_form: function() {
					var form = $('<form></form>');
					var name = $("<input type='text' name='name' style='display:none' value='" + TREE.parent_name + "' class='small_edit'/>");
					var title = $("<input type='text' name='title' placeholder='名字' class='small_edit'/>");
					var confirm = $("<a class='small_btn'>添加</a>");
					var cancel = $("<a class='small_btn cancel'>取消</a>");
					form.append(title);
					form.append(name);
					form.append(confirm);
					form.append(cancel);
					confirm.click(function() {
						var title = $(this).closest('form').find('input[name="title"]').val();
						var name = $(this).closest('form').find('input[name="name"]').val();
						if (title == '') {
							system_error_hint("名称不能为空");
							return;
						}
						con_fun(form);
					});
					cancel.click(function() {
						can_fun(form);
					});
					return form;
				},
				edit_form: function(item) {
					var form = $('<form></form>');
					var name = $("<input type='text' name='name' value='" + item.name + "' placeholder='权限' class='small_edit'/>");
					var title = $("<input type='text' name='title' value='" + item.title + "' placeholder='名字' class='small_edit'/>");
					var confirm = $("<a class='small_btn'>修改</a>");
					var cancel = $("<a class='small_btn cancel'>取消</a>");
					form.append(title);
					form.append(name);
					form.append(confirm);
					form.append(cancel);
					confirm.click(function(e) {
						var title = $(this).closest('form').find('input[name="title"]').val();
						var name = $(this).closest('form').find('input[name="name"]').val();
						if (title == '') {
							system_error_hint("名称不能为空");
							return;
						}
						if (name == '') {
							system_error_hint("权限不能为空");
							return;
						}
						con_fun(form);
						e.stopPropagation();
					});
					cancel.click(function(e) {
						can_fun(form);
						e.stopPropagation();
					});
					return form;
				},
				search_form: function(item) {
					var form = $('<form></form>');
					var name = $("<input type='text' disabled  value='" + item.name + "' placeholder='权限'  class='small_edit'/>");
					var title = $("<input type='text' disabled value='" + item.title + "' placeholder='名字' class='small_edit'/>");
					var cancel = $("<a class='small_btn cancel'>返回</a>");
					form.append(title);
					form.append(name);
					form.append(cancel);
					cancel.click(function(e) {
						con_fun(form);
						e.stopPropagation();
					});
					return form;
				}
			}
		},
		careateDom: function($item, $ul) {
			$.each(TREE.jsonObj, function(ids, item) {
				if ($item.id == item.pid) {
					if (TREE.is_have_child(item.id)) {
						var $item_ = TREE.create_li.create_item(item).parent_item();
						var $ul_ = TREE.create_li.create_item(item).li_ul();
						$ul_.appendTo($item_);
						$item_.appendTo($ul);
						TREE.careateDom(item, $ul_);
					} else if (item.name == TREE.parent_name) {
						var $item_ = TREE.create_li.create_item(item).parent_item();
						var $ul_ = TREE.create_li.create_item(item).li_ul();
						$ul_.appendTo($item_);
						$item_.appendTo($ul);
					} else {
						var $item_ = TREE.create_li.create_item(item).chlid_item();
						$item_.appendTo($ul);
					}
				}
			});
		},
		/*判断当前数据对象是不是父类对象
		 *item:每个权限的数据对象
		 */
		is_have_child: function(pid) {
			var i = 0;
			$.each(TREE.jsonObj, function(ids, item) {
				if (item.pid == pid) {
					i = 1;
					return;
				}
			});
			if (i == 0) {
				return false;
			} else {
				return true;
			}
		},
		create_li: {
			create_item: function(item) {
				//创建工具条
				var create_tool = {
					/*点击添加按钮显示出来的按钮*/
					create_add_view: function() {
						var add_item = $('<a href="javascript:;" class="add_authority" data-id=' + item.id + '>权限</a>').addClass(TREE.c_item.tool.add.c_choose.item);
						var add_root = $('<a href="javascript:;" class="add_group" data-id=' + item.id + '>分组</a>').addClass(TREE.c_item.tool.add.c_choose.root);
						var add = $('<span class="tool_add" style="display:none"></span>').addClass(TREE.c_item.tool.add.c_choose.c_);
						add_item.appendTo(add);
						add_root.appendTo(add);
						return add;
					},
					/*点击删除按钮显示出来的按钮*/
					create_del_view: function() {
						var del_item = $('<a href="javascript:;" data-id=' + item.id + '>确定删除？</a>').addClass(TREE.c_item.tool.del.c_confirm.item);
						var del = $('<span class="tool_add" style="display:none"></span>').addClass(TREE.c_item.tool.del.c_confirm.c_);
						del_item.appendTo(del);
						return del;
					},
					/*父节点的工具条*/
					create_tool: function() {
						var add = $('<i class="icon-plus-sign font-grade-one " data-id=' + item.id + '></i>').addClass(TREE.c_item.tool.add.c_);
						var del = $('<i class="icon-trash font-grade-one " data-id=' + item.id + '></i>').addClass(TREE.c_item.tool.del.c_);
						var edit = $('<i class="icon-edit font-grade-one  " data-id=' + item.id + '></i>').addClass(TREE.c_item.tool.edit.c_);
						var search = $('<i class="icon-search font-grade-one " data-id=' + item.id + '></i>').addClass(TREE.c_item.tool.search.c_);
						var tool = $('<span class="tool" style="visibility:hidden"></span>').addClass(TREE.c_item.tool.c_);
						add.appendTo(tool);
						del.appendTo(tool);
						edit.appendTo(tool);
						search.appendTo(tool);
						return tool;
					},
					/*子节点的工具条*/
					child_tool: function() {
						var add = $('<i class="icon-plus-sign font-grade-one disabled" disabled data-id=' + item.id + '></i>').addClass(TREE.c_item.tool.add.c_);
						var del = $('<i class="icon-trash font-grade-one " data-id=' + item.id + '></i>').addClass(TREE.c_item.tool.del.c_);
						var edit = $('<i class="icon-edit font-grade-one  " data-id=' + item.id + '></i>').addClass(TREE.c_item.tool.edit.c_);
						var search = $('<i class="icon-search font-grade-one " data-id=' + item.id + '></i>').addClass(TREE.c_item.tool.search.c_);
						var tool = $('<span class="tool" style="visibility:hidden"></span>').addClass(TREE.c_item.tool.c_);
						add.appendTo(tool);
						del.appendTo(tool);
						edit.appendTo(tool);
						search.appendTo(tool);
						return tool;
					}
				};
				/*创建节点的主要内容*/
				var create_main = {
					root_li: function() {
						return $('<span class="parent_li item"><i class="icon-folder-open"></i> ' + item.title + '</span>').addClass(TREE.c_item.c_).addClass(TREE.c_item.c_parent);
					},
					parent_li: function() {
						return $('<span class="parent_li item"><i class="icon-minus-sign"></i> ' + item.title + '</span>').addClass(TREE.c_item.c_).addClass(TREE.c_item.c_parent);
					},
					chlid_li: function() {
						return $('<span class="item"><i class="icon-leaf"></i> ' + item.title + '</span>').addClass(TREE.c_item.c_).attr('data',item.id);
					},
					null_li: function() {
						return $('<span class="item">').addClass(TREE.c_item.c_);
					},
					li_ul: function() {
						return $('<ul></ul>');
					}
				};
				return {
					root_item: function() {
						var $li = $('<li></li>');
						create_main.root_li().appendTo($li);
						create_tool.create_tool().appendTo($li);
						create_tool.create_add_view().appendTo($li);
						create_tool.create_del_view().appendTo($li);
						return $li;
					},
					parent_item: function() {
						var $li = $('<li></li>');
						create_main.parent_li().appendTo($li);
						create_tool.create_tool().appendTo($li);
						create_tool.create_add_view().appendTo($li);
						create_tool.create_del_view().appendTo($li);
						return $li;
					},
					chlid_item: function() {
						var $li = $('<li></li>');
						create_main.chlid_li().appendTo($li);
						create_tool.child_tool().appendTo($li);
						create_tool.create_del_view().appendTo($li);
						return $li;
					},
					null_item: function() {
						var $li = $('<li></li>');
						create_main.null_li().appendTo($li);
						return $li;
					},
					li_ul: function() {
						return create_main.li_ul();
					}
				};
			}
		},
		/*给当前的工具条设为不可以使用*/
		disable_tool: {
			//离开
			off: function($tool) {
				//$tool.find('>i').addClass('disabled');
			},
			//进入
			on: function($tool) {
				//	$tool.find('>i').removeClass('disabled');
			}
		}
	};
	TREE.listener();//对这个树做全局监听
	TREE.choose_listener();
	
	window.yun_tree = TREE;
});